package com.feihong.txgw.adapter.Holder;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.feihong.txgw.R;
import com.feihong.txgw.http.Host;
import com.feihong.txgw.json.HomeJSON;
import com.feihong.txgw.model.SerializableHashMap;
import com.feihong.txgw.ui.activity.HomeActivity;
import com.feihong.txgw.ui.activity.base.WebViewActivity;
import com.feihong.txgw.ui.activity.kaika.KaikaHomeActivity;
import com.feihong.txgw.ui.activity.kuandai.KuanDaiYuYueActivity;
import com.feihong.txgw.ui.activity.mendian.FuJinMenDianActivity;
import com.feihong.txgw.ui.activity.recharge.RechargeActivity;
import com.feihong.txgw.ui.activity.user.LiuLiangChaXunActivity;
import com.feihong.txgw.ui.activity.user.LoginActivity;
import com.feihong.txgw.utils.UserLoginInfo;
import com.feihong.txgw.view.ScrollUpAdvert.BaseAutoScrollUpTextView;
import com.feihong.txgw.view.ScrollUpAdvert.MainScrollUpAdvertisementView;

import java.util.HashMap;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by Sabirjan on 2017/6/15.
 * TXGW.
 * 飞宏电子
 */

public class HomeMenuHolder extends BaseViewHolder implements View.OnClickListener {

    private Context mContext;
    private HomeJSON.HomeData homeJSON;
//    private TextView txt_message;
    private View lay_tongzhi;
//    private TextView txt_message_type;
    private MainScrollUpAdvertisementView mScrollUpAdvertLayout;
    public HomeMenuHolder(Context context, View itemView) {
        super(itemView);

        this.mContext = context;
        mScrollUpAdvertLayout = (MainScrollUpAdvertisementView) itemView.findViewById(R.id.main_advertisement_view);
//        txt_message = (TextView) itemView.findViewById(R.id.txt_message);
//        txt_message_type = (TextView) itemView.findViewById(R.id.txt_message_type);
        lay_tongzhi = itemView.findViewById(R.id.lay_tongzhi);
        itemView.findViewById(R.id.txt_fujin_mendian).setOnClickListener(this);
        itemView.findViewById(R.id.txt_shouji_kaika).setOnClickListener(this);
        itemView.findViewById(R.id.txt_liuliang_chaxun).setOnClickListener(this);
        itemView.findViewById(R.id.txt_huafei_chongzhi).setOnClickListener(this);
        itemView.findViewById(R.id.txt_kuandai_yuyue).setOnClickListener(this);
    }

    public void bindData(HomeJSON.HomeData data) {
        this.homeJSON = data;
        if (homeJSON == null) {
            return;
        }
        if (homeJSON.getTopdata() != null && homeJSON.getTopdata().getMenuitems() != null) {
            for (HomeJSON.TopdataBean.MenuitemsBean item : homeJSON.getTopdata().getMenuitems()) {
                if (item != null && item.getAction() != null && item.getName() != null) {

                    if (item.getAction().toLowerCase().contentEquals("mendian"))//附近门店
                    {
                        if (item.getStatus().contentEquals("0")) {
                            itemView.findViewById(R.id.txt_fujin_mendian).setEnabled(false);
                        }
                        continue;
                    }
                    if (item.getAction().toLowerCase().contentEquals("kiaka"))//手机开卡
                    {
                        if (item.getStatus().contentEquals("0")) {
                            itemView.findViewById(R.id.txt_shouji_kaika).setEnabled(false);
                        }
                        continue;
                    }
                    if (item.getAction().toLowerCase().contentEquals("yuliang"))//余量查询
                    {
                        if (item.getStatus().contentEquals("0")) {
                            itemView.findViewById(R.id.txt_liuliang_chaxun).setEnabled(false);
                        }
                        continue;
                    }
                    if (item.getAction().toLowerCase().contentEquals("chongzhi"))//话费充值
                    {
                        if (item.getStatus().contentEquals("0")) {
                            itemView.findViewById(R.id.txt_huafei_chongzhi).setEnabled(false);
                        }
                        continue;
                    }
                    if (item.getAction().toLowerCase().contentEquals("kuandai"))//话费充值
                    {
                        if (item.getStatus().contentEquals("0")) {
                            itemView.findViewById(R.id.txt_kuandai_yuyue).setEnabled(false);
                        }
                        continue;
                    }

                }
            }
        }


        if (homeJSON == null || homeJSON.getTopdata() == null || homeJSON.getTopdata().getMessage() == null) {
            lay_tongzhi.setVisibility(View.GONE);
        } else {
            lay_tongzhi.setVisibility(View.VISIBLE);
            final List<HomeJSON.TopdataBean.MessageBean> Msgs = homeJSON.getTopdata().getMessage();
            mScrollUpAdvertLayout.setData(Msgs);
            mScrollUpAdvertLayout.setTextSize(13);
            mScrollUpAdvertLayout.setOnItemClickListener(new BaseAutoScrollUpTextView.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Intent intent = new Intent();
                    intent.setClass(mContext, WebViewActivity.class);
                    HashMap map = new HashMap();
                    map.put("id",Msgs.get(position).getId());
                    SerializableHashMap serializableHashMap = new SerializableHashMap();
                    serializableHashMap.setMap(map);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(WebViewActivity.INTENT_PARAMS,serializableHashMap);
                    bundle.putString(WebViewActivity.INTENT_TITLE,"公告详情");
                    bundle.putString(WebViewActivity.INTENT_WEBURL,Host.HTTP_HOME + Msgs.get(position).getUrl() + "?");
                    intent.putExtras(bundle);
//                    intent.putExtra(WebViewActivity.INTENT_WEBURL, Host.HTTP_HOME+Msgs.get(position).getUrl()+"?"+new TokenUtils().setSigen(map));
//                    intent.putExtra(WebViewActivity.INTENT_TITLE,"消息详情");
                    mContext.startActivity(intent);
                }
            });
            mScrollUpAdvertLayout.setTimer(2000);
            mScrollUpAdvertLayout.start();
//            lay_tongzhi.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    Intent intent = new Intent();
//                    intent.setClass(mContext, WebViewActivity.class);
//                    intent.putExtra(WebViewActivity.INTENT_WEBURL, Msgs.get(p));
//                    intent.putExtra(WebViewActivity.INTENT_TITLE,"消息详情");
//                    mContext.startActivity(intent);
//
//                }
//            });
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        Intent intent = new Intent();
        switch (id) {
            case R.id.txt_fujin_mendian:
                //附近店面
                intent.setClass(mContext, FuJinMenDianActivity.class);
                break;
            case R.id.txt_huafei_chongzhi:
                //话费充值
                intent.setClass(mContext, RechargeActivity.class);
                break;
            case R.id.txt_shouji_kaika:
                //手机开卡
                intent.setClass(mContext, KaikaHomeActivity.class);
                break;
            case R.id.txt_liuliang_chaxun:
                //流量充值
                intent.setClass(mContext, LiuLiangChaXunActivity.class);
                break;
            case R.id.txt_kuandai_yuyue:
                intent.setClass(mContext,KuanDaiYuYueActivity.class);
                break;
            default:break;
        }
        if (id == R.id.txt_fujin_mendian) {
            mContext.startActivity(intent);
        } else {
            if (UserLoginInfo.isUserLogin()) {
                mContext.startActivity(intent);
            } else {

                HomeActivity homeActivity = (HomeActivity) mContext;
                homeActivity.showConfirmDialog("未登录", "您还未登录，请先登录！", "登录", "取消",
                        new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                sweetAlertDialog.dismiss();
                                Intent loginIntent = new Intent();
                                loginIntent.setClass(mContext, LoginActivity.class);
                                mContext.startActivity(loginIntent);

                            }
                        });
            }
        }
    }
}
